<?php

/**
 * Action Helper for getting the extension of a file
 *
 * @uses Zend_Controller_Action_Helper_Abstract
 */
class Helper_FileExtension extends Zend_Controller_Action_Helper_Abstract
{
    /**
     * Returns the file extension according to its mime type
     *
     * @param  mixed $type
     * @return string
     */
    public function getFileExtension($fileInfo)
    {
        $ext = '';
        $type = (is_array($fileInfo) ? $fileInfo['type'] : $fileInfo);

        if (preg_match('`/([a-z]+)$`', $type, $matches) > 0) {
            $ext = $matches[1];
        } else {
            return 'no type found';
        }

        switch ($ext)
        {
            case 'jpeg':
                $ext = 'jpg';
                break;
        }

        return $ext;
    }

    /**
     * Strategy pattern: call helper as broker method
     * For example:
     * $this->_helper->formLoader->load('login', 'default');
     *
     * @param  string $name
     * @return Zend_Form
     */
    public function direct($type)
    {
        return $this->getFileExtension($type);
    }
}
